package com.example.cks_show_text;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.database.DatabaseUtils;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.example.cks_show_text.databinding.ActivityMainBinding;
import com.example.cks_show_text.model.CksShow;
import com.example.cks_show_text.usb.FaceHandlerUSB;
import com.example.cks_show_text.usb.USBBack;
import com.example.cks_show_text.util.ByteUtil;

import static com.example.cks_show_text.MainActivity.*;

public class MainActivity extends AppCompatActivity implements USBBack {

    ActivityMainBinding binding;
    CksShow cksShow = new CksShow("1");
    private String TAG = MainActivity.class.getSimpleName();
    int serial = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.setCksshow(cksShow);
        findViewById(R.id.set).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        FaceHandlerUSB.getInstance();
        FaceHandlerUSB.getInstance().setBackLinsten(this);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Log.d(TAG, "run: ");
                    byte[] face_order = {0x01, 0x03, 0x00, 0x00, 0x00, 0x01, (byte) 0x84, 0x0a};
                    FaceHandlerUSB.getInstance().send(face_order);
                }
            }
        }).start();
    }
    @Override
    protected void onStop() {
        super.onStop();
        FaceHandlerUSB.getInstance().stopusb();
    }


    @Override
    public void bakc(byte[] bytes) {
        String d = ByteUtil.byteArray2HexString(bytes);
        Log.d(TAG, "bakc: "+d);
        if(d.length()==14){
            int serial1 = Integer.parseInt(d.substring(8,10));
            if(serial1 !=serial){
                Log.d(TAG, "bakc: "+serial);
                cksShow = new CksShow(""+serial1);
                serial = serial1;
                binding.setCksshow(cksShow);
            }
        }
    }
}